package com.base.myproject.client.ui;

import com.extjs.gxt.ui.client.widget.form.Field;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Image;

public class FormImage extends Field{
	Image image;
	  private String text;

	  /**
	   * Creates a new label field.
	   */
	  public FormImage() {
	    baseStyle = "x-form-label";
	    setLabelSeparator("");
	    image = new Image();
	  }

	  /**
	   * Creates a new label field.
	   * 
	   * @param text the label text
	   */
	  public FormImage(String text) {
	    this();
	    setText(text);
	  }

	  /**
	   * Returns the field's text.
	   * 
	   * @return the text
	   */
	  public String getText() {
	    return text;
	  }

	  @Override
	  public Object getValue() {
	    return text;
	  }

	  @Override
	  public boolean isValid() {
	    return true;
	  }

	  @Override
	  public void markInvalid(String msg) {

	  }

	  /**
	   * Sets the lable's text.
	   * 
	   * @param text the text as HTML
	   */
	  public void setText(String text) {
	    this.text = text;
	    if (rendered) {
	    	image.setUrl(text);
	    	System.out.println(image);
	      getElement().setInnerHTML(image.toString());
	    }
	  }

	  @Override
	  public void setValue(Object value) {
	    setText(value != null ? value.toString() : "");
	  }

	  @Override
	  public boolean validate() {
	    return true;
	  }

	  @Override
	  protected void onRender(Element parent, int index) {
	    setElement(DOM.createDiv(), parent, index);
	    if (text != null) {
	      setText(text);
	    }
	  }

	  @Override
	  protected boolean validateValue(String value) {
	    return true;
	  }
	  
	  @Override
	  protected void onResize(int width, int height) {
	    el().setSize(width, height, true);
	    super.onResize(width, height);
	  }

}
